﻿//TODO: check filter, SkinFilter2

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// Skin Filter
    /// </summary>
    public class SkinFilter2 : BaseForeBackgroundColorFilter
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="SkinFilter2"/> class.
        /// </summary>
        public SkinFilter2()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="SkinFilter2"/> class.
        /// </summary>
        /// <param name="_skinFilter">The _skin filter.</param>
        internal SkinFilter2(SkinFilter2 _skinFilter)
        {
        }

        /// <summary>
        /// Execute this Filter
        /// A Debug Statement is written if a concrete Filter is called
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            UnsafeBitmap bitmap = _bitmap;

            int nHeight = bitmap.Height;
            int nWidth = bitmap.Width;

            int y = 0;
            int x = 0;

            Color color = Color.Transparent;

            for (y = 0; y < nHeight; y++)
            {
                for (x = 0; x < nWidth; x++)
                {
                    color = bitmap.GetPixel(x, y);
                    float B = color.B;
                    float G = color.G;
                    float R = color.R;

                    // R G and B values are presented in the range 0 to 255
                    if ((B > 160 && R < 180 && G < 180) || // Too much blue
                    (G > 160 && R < 180 && B < 180) || // Too much green
                    (B < 100 && R < 100 && G < 100) || // Too dark
                    (G > 200) || // Green
                    (R + G > 400) || // Too much red and green (yellow like color)
                    (G > 150 && B < 90) || // Yellow like also
                    (B / (R + G + B) > .40) || // Too much blue in contrast to others
                    (G / (R + G + B) > .40) || // Too much green in contrast to others
                    (R < 102 && G > 100 && B > 110 && G < 140 && B < 160) // Ocean
                    )
                    {
                        bitmap.SetPixel(x, y, m_ColorBackground);
                    }
                }
            }
            return _bitmap;
        }

        /// <summary>
        /// This is used to clone a filter, each filter must implement it's own clone functionality
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new SkinFilter2(this);
        }
    }
}
